#include <iostream>
#include <cstdlib>
#include <cstring>
#define MAX(A,B) A>B?A:B
using namespace std;

struct Medicine{
	int costTime;
	int value;
};

int main(){
	int totalTime;
	int medicineNum;
	cin >> totalTime >> medicineNum;
	Medicine medicine[200]; 
	for(int i = 1; i <= medicineNum; i++)
		cin >> medicine[i].costTime >> medicine[i].value;
	int valueOfTime[200][2000];
	for(int i = 1; i <= medicineNum; i++)
		valueOfTime[i][0] = 0;
	memset(valueOfTime[medicineNum+1],0,1000*sizeof(int));
	for(int i = medicineNum; i >= 1; i--){
		for(int j = 1; j <= totalTime; j++){
			int maxValue = valueOfTime[i+1][j];
			if(j >= medicine[i].costTime) maxValue = MAX(maxValue,valueOfTime[i+1][j-medicine[i].costTime] + medicine[i].value);
			valueOfTime[i][j] = maxValue;
		}
	}
	cout << valueOfTime[1][totalTime] << endl;
	return 0;
}
